package com.peakmain.homemodule.activity;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.blankj.utilcode.util.BarUtils;
import com.peakmain.baselibrary.widget.view.ShapeTextView;
import com.peakmain.homemodule.R;
import com.peakmain.homemodule.adapter.SearchCityAdapter;
import com.peakmain.homemodule.adapter.decoration.SectionItemDecoration;
import com.peakmain.homemodule.base.BaseHomeActivity;
import com.peakmain.homemodule.bean.SearchCityBean;
import com.peakmain.homemodule.utils.CityCompartor;
import com.peakmain.homemodule.utils.PinyinUtil;
import com.peakmain.homemodule.view.SlideIndexBar;
import com.peakmain.wanandroid.netmodule.constant.Constant;
import com.peakmain.wanandroid.netmodule.net.http.util.JsonUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * author ：Peakmain
 * version : 1.0
 * createTime：2020/2/28
 * mail:2726449200@qq.com
 * describe：搜索城市activity
 */
public class SearchCityActivity extends BaseHomeActivity implements SlideIndexBar.OnIndexTouchedChangedListener {
    private RecyclerView mRvSearchCity;
    private SlideIndexBar mSlideIndexBar;
    private ShapeTextView mShapeTextView;
    private List<SearchCityBean> mAllCities = new ArrayList<>();
    private SearchCityAdapter mSearchCityAdapter;
    private LinearLayoutManager mLayoutManager;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search_city;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initView() {
        //设置状态栏颜色
        mRvSearchCity = findViewById(R.id.rv_search_city);
        mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRvSearchCity.setLayoutManager(mLayoutManager);

        mSlideIndexBar = findViewById(R.id.slide_index_bar);
        mSlideIndexBar.setNavigationBarHeight(BarUtils.getStatusBarHeight());
        String[] indexs = {"定位", "热门"};
        mSlideIndexBar.addFirstIndexItem(indexs);
        mShapeTextView = findViewById(R.id.shape_text_view);
        mSlideIndexBar.setOverlayTextView(mShapeTextView).setOnIndexChangedListener(this);

        //模拟
        String cityStr = JsonUtil.getAssetsJson(Constant.FILE_NAME_CITY_LIST);
        try {
            JSONObject jsonObject = new JSONObject(cityStr);
            mAllCities = JsonUtil.parseArrList(jsonObject.getJSONArray("list").toString(), SearchCityBean.class);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        for (SearchCityBean city : mAllCities) {
            String pinyin = PinyinUtil.getPingYin(city.getName());
            city.setPinyin(pinyin);
            //首字母
            String sortString = pinyin.substring(0, 1).toUpperCase();
            if (sortString.matches("[A-Z]")) {
                city.setSortLetters(sortString.toUpperCase());
            } else {
                city.setSortLetters("#");
            }
        }
       //排序
        CityCompartor cityCompartor = new CityCompartor();
        Collections.sort(mAllCities,cityCompartor);

        mSearchCityAdapter = new SearchCityAdapter(this, mAllCities);
        mSearchCityAdapter.setLayoutManager(mLayoutManager);
       // SectionItemDecoration decoration = new SectionItemDecoration(this, mAllCities);
        SectionItemDecoration decoration = new SectionItemDecoration.Builder(this, mAllCities)
                .setBgColor(ContextCompat.getColor(this,R.color.colorAccent))
                .setTextColor(ContextCompat.getColor(this,R.color.color_black))
                .create();
        mRvSearchCity.addItemDecoration(decoration, 0);
        mRvSearchCity.setAdapter(mSearchCityAdapter);
    }


    @Override
    public void onIndexChanged(String word, int position) {
        mSearchCityAdapter.scrollToSection(word);
    }
}
